package com.ljc.service;

import com.ljc.dao.CollegeDao;
import com.ljc.dao.UserDao;
import com.ljc.entity.College;
import com.ljc.vo.CollegeSearch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CollegeServiceImpl implements CollegeService{
    @Autowired
    private CollegeDao collegeDao;

    @Autowired
    private UserDao userDao;
    @Override
    public List<College> getCollege(int deanId) {
        return collegeDao.getCollege(deanId);
    }

    @Override
    public List<College> searchCollege(CollegeSearch collegeSearch) {
        return collegeDao.searchCollege(collegeSearch);
    }

    @Override
    public void addCollege(College college) {
        College collegeDB = collegeDao.findByName(college.getName());
        if (collegeDB!=null){
            throw  new RuntimeException("该学院名已经存在，请重新尝试别的名称");
        }else {
            collegeDao.addCollege(college);
        }
    }

    @Override
    public College getCollegeById(int id) {
        return collegeDao.getCollegeById(id);
    }

    @Override
    public void updateCollege(College college) {
        this.collegeDao.updateCollege(college);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void delete(int id) {
        userDao.deletedByCollegeId(id);
        collegeDao.delete(id);
    }
}
